package codeRandomThoughts.Test6234最小公倍数为K的子数组数目;


public class Solution {
    public static int subarrayLCM(int[] nums, int k) {
        int lcm = 1;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            lcm = 1;
            for (int j = i; j < nums.length; j++) {
                lcm = getLcm(lcm, nums[j]);
                if (lcm == k) {
                    count++;
                } else if (lcm > k) {
                    break;
                }
            }
        }

        return count;
    }

    public static int getLcm(int num1, int num2) {
        int multiply = num1 * num2;
        int tem = 0;
        if (num1 > num2) {
            tem = num2;
            num2 = num1;
            num1 = tem;
        }
        for (int i = num2; i <= multiply; i++) {
            if (i % num1 == 0 && i % num2 == 0) {
                return i;
            }
        }
        return multiply / num2;
    }

//    public static void main(String[] args) {
//        int[] nums = {3, 20, 1, 12, 3, 16, 16
//        };
//        int k = 18;
//    }
}
